#ifndef __CH32V_DEF_H__
#define __CH32V_DEF_H__

#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
/**
 * @brief STM32 core version number
 */
#define CH32V_CORE_VERSION_MAJOR    (0x01U) /*!< [31:24] major version */
#define CH32V_CORE_VERSION_MINOR    (0x09U) /*!< [23:16] minor version */
#define CH32V_CORE_VERSION_PATCH    (0x00U) /*!< [15:8]  patch version */

/*
 * Extra label for development:
 * 0: official release
 * [1-9]: release candidate
 * F[0-9]: development
 */
#define CH32V_CORE_VERSION_EXTRA    (0x00U) /*!< [7:0]  extra version */
#define CH32V_CORE_VERSION          ((CH32V_CORE_VERSION_MAJOR << 24U)\
                                        |(CH32V_CORE_VERSION_MINOR << 16U)\
                                        |(CH32V_CORE_VERSION_PATCH << 8U )\
                                        |(CH32V_CORE_VERSION_EXTRA))


#ifdef CH32V0xx
    #include <ch32v00x.h>
#elif defined(CH32V1xx)
    #include <ch32v10x.h>
#elif defined(CH32V3xx)
    #include <ch32v30x.h>
#else
    #error "chip series is not defined in boards.txt."
#endif

#define UNUSED(X) (void)X 

#ifndef F_CPU
  #define F_CPU SystemCoreClock
#endif

/**
 * Libc porting layers
 */
#if defined (  __GNUC__  ) /* GCC CS3 */
  #define WEAK __attribute__ ((weak))
  #if defined(__FREERTOS__)
    #define __func_interrupt    __attribute__ ((interrupt ()))
  #elif (__GNUC__ == 8 || __GNUC__ == 12) && (__GNUC_MINOR__ == 2) 
    #define __func_interrupt    __attribute__ ((interrupt ("WCH-Interrupt-fast")))
  #else
    #define __func_interrupt    __attribute__ ((interrupt ("machine")))
  #endif
  #ifndef __weak
  #define __weak   __attribute__((weak))
  #endif /* __weak */
  #ifndef __packed
  #define __packed __attribute__((__packed__))
  #endif /* __packed */

#endif

#ifndef __no_init
#define __no_init __attribute__((unused, section(".noinit")))
#endif

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

// weaked functions declaration
void SystemClock_Config(void);

void _Error_Handler(const char *, int);

#define Error_Handler() _Error_Handler(__FILE__, __LINE__)

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus

#endif // __CH32Vc_DEF_H__